package com.example.application1.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.application1.Entity.Bean;
import com.example.application1.Entity.NewslistDTO;
import com.example.application1.R;

import java.util.List;

public class newsAdapter extends RecyclerView.Adapter<newsAdapter.newsHolder> {
    private List<NewslistDTO> newsList;
    private Context context;
    public newsAdapter( List<NewslistDTO> newsList,Context context) {
        this.newsList = newsList;
        this.context=context;
    }

    @NonNull
    @Override
    public newsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new newsHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_normal,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull newsHolder holder, int position) {
        Glide.with(context)
                .load(newsList.get(position).getPicUrl())
                .centerCrop()
                .placeholder(R.drawable.defaultbg)
                .into(holder.itemViewImage);
        holder.itemTextTitle.setText(newsList.get(position).getTitle());
        holder.itemTextSource.setText(newsList.get(position).getSource());
        holder.itemTextTime.setText(newsList.get(position).getCtime());
    }

    @Override
    public int getItemCount() {
        return newsList == null ? 0 : newsList.size();
    }

    public  void refresh(List<NewslistDTO> artists){
//      对适配器刷新
        this.newsList=artists;
        notifyDataSetChanged();
        Log.d("lcj","notifyDataSetChanged");
    }

    public class newsHolder extends RecyclerView.ViewHolder {
        ImageView itemViewImage;
        TextView itemTextTitle;
        TextView itemTextTime;
        TextView itemTextSource;
        public newsHolder(@NonNull View itemView) {
            super(itemView);
            itemViewImage = itemView.findViewById(R.id.item_news_tv_img);
            itemTextTitle = itemView.findViewById(R.id.item_news_tv_title);
            itemTextTime = itemView.findViewById(R.id.item_news_tv_time);
            itemTextSource=itemView.findViewById(R.id.item_news_tv_source);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener != null) {
                        monItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

        private OnRecyclerItemClickListener monItemClickListener;

        public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
            monItemClickListener = listener;
        }

        public interface OnRecyclerItemClickListener {
            void onRecyclerItemClick(int position);
        }

    }

